#ifndef _DISK_H_
#define _DISK_H_

#include "file.h"
#include "folder.h"
#include "filesystemerror.h"

#include <QObject>
#include <QList>
#include <QString>
#include <QDir>


namespace Avd {

class Daemon;
class Action;

/**
 * Disk class.
 * Handle modifications on filesystem like: save, remove, update...
 */

class Disk : public QObject, public Folder {
Q_OBJECT
public:
	/**
	 * Unique Constructor
	 */
	Disk(const QString& path, Daemon* daemon);

	/**
	 * Destructor.
	 */
  ~Disk();

	/**
	 * Find modifications and inconsistencies.
	 * Check if actual disk is equal to server disk.
	 * @return a list of actions to execute.
	 * @see Action class.
	 */
	QList<Action*> check();

	/**
	 * Save file. Write on filesystem if saveData is enabled.
	 * @param file file that will be saved
	 * @param saveData write or not on disk.
	 */
	void save(File& file, const bool saveData = true) throw(FileSystemError);

	void remove(const File& file) throw(FileSystemError);
	void remove(const Folder& folder) throw(FileSystemError);

	/**
	 * Move a filesystem entry.
	 * @param oldPath old path related to a file or a folder
	 * @param newPath
	 */
	void move(const QString& oldPath, const QString& newPath) throw(FileSystemError);
	void move(const Folder& oldFolder, const Folder& newFolder) throw(FileSystemError);
	void move(const File& oldFile, const File& newFile) throw(FileSystemError);
	void update(File& file, const bool saveData = true) throw(FileSystemError);
	void create(const Folder& folder) throw(FileSystemError);

	QString getAbsolutePath() { return absolutePath; }
private:
	/**
	 * Copying Disk objects not allowed.
	 */
	Disk(const Disk& disk);

	bool merge(File& file);

	/**
	 * Helper function.
	 * @param path a string representing a path(ex: "/home//dev/stats/../stats//file/")
	 * @return A clean path.(ex: "/home/dev/stats/file")
	 */
	inline QString cleanPath(const QString& path);

	QString absolutePath;
	Daemon* daemon;
};

QString Disk::cleanPath(const QString& path) {
	return QDir::cleanPath(path);
}

}

#endif // _DISK_H_
